package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

// 干扰主神状态
public class DistractState implements GameState {
    private final CodeRebirthGame game;
    private JTextArea codeArea;
    private JLabel challengeLabel;
    private JLabel scoreLabel;
    private int score;

    public DistractState(CodeRebirthGame game) {
        this.game = game;
        this.score = game.getPlayerData().getScore();
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());

        // 挑战面板
        JPanel challengePanel = new JPanel(new BorderLayout());
        challengePanel.setBackground(new Color(255, 0, 0, 100)); // 40%透明红色
        challengePanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        challengeLabel = new JLabel(
                "<html><div style='width:600px;color:white;font-weight:bold;'>" +
                        "干扰主神挑战: 编写代码迷惑主神系统" +
                        "</div></html>"
        );
        challengePanel.add(challengeLabel, BorderLayout.CENTER);

        // 代码编辑区
        codeArea = new JTextArea();
        codeArea.setFont(game.getResourceManager().getFont("code", 16f));
        codeArea.setBackground(Color.BLACK);
        codeArea.setForeground(new Color(150, 255, 150)); // 荧光绿
        codeArea.setCaretColor(Color.WHITE);
        codeArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 底部按钮面板
        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.setBackground(Color.BLACK);

        // 左侧积分显示
        JPanel leftPanel = new JPanel();
        leftPanel.setOpaque(false);
        scoreLabel = new JLabel("积分: " + score);
        scoreLabel.setForeground(Color.WHITE);
        leftPanel.add(scoreLabel);

        // 右侧按钮
        JPanel rightPanel = new JPanel();
        rightPanel.setOpaque(false);

        JButton saveBtn = new JButton("保存进度");
        styleButton(saveBtn);
        saveBtn.addActionListener(e -> game.getPlayerData().saveGame());

        JButton hintBtn = new JButton("提示 (10积分)");
        styleButton(hintBtn);
        hintBtn.addActionListener(e -> {
            if (score >= 10) {
                score -= 10;
                scoreLabel.setText("积分: " + score);
                showMessage("提示: 使用递归函数迷惑系统");
            } else {
                showMessage("积分不足!");
            }
        });

        JButton submitBtn = new JButton("提交");
        styleButton(submitBtn);
        submitBtn.addActionListener(e -> checkAnswer());

        rightPanel.add(saveBtn);
        rightPanel.add(Box.createHorizontalStrut(10));
        rightPanel.add(hintBtn);
        rightPanel.add(Box.createHorizontalStrut(10));
        rightPanel.add(submitBtn);

        bottomPanel.add(leftPanel, BorderLayout.WEST);
        bottomPanel.add(rightPanel, BorderLayout.EAST);

        // 添加到主界面
        game.add(challengePanel, BorderLayout.NORTH);
        game.add(new JScrollPane(codeArea), BorderLayout.CENTER);
        game.add(bottomPanel, BorderLayout.SOUTH);
    }

    private void checkAnswer() {
        String answer = codeArea.getText().trim();
        if (answer.isEmpty()) {
            showMessage("请输入代码!");
            return;
        }

        // 简单模拟答案检查
        if (answer.contains("recursive")) {
            // 答案正确
            game.changeState(new RepairState(game));
        } else {
            // 答案错误 - 游戏结束
            game.changeState(new GameOverState(game));
        }
    }

    private void styleButton(JButton button) {
        button.setFont(game.getResourceManager().getFont("main", 14f));
        button.setBackground(new Color(200, 50, 50));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(game, msg);
    }

    @Override
    public void render(Graphics g) {
        // 背景已经在组件中处理
    }

    @Override public void update() {}
    @Override public void exit() { game.getContentPane().removeAll(); }
    @Override public void handleInput(KeyEvent e) {}
}
